package com.yike.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yike.common.exception.BusinessException;
import com.yike.common.tool.LoginUtil;
import com.yike.product.entity.Category;
import com.yike.product.mapper.CategoryMapper;
import com.yike.product.param.CategoryParam;
import com.yike.product.service.CategoryService;
import com.yike.product.param.CategoryPageParam;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.product.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 商品分类 服务实现类
 *
 * @author Sakura
 * @since 2024-09-02
 */
@Slf4j
@Service
public class CategoryServiceImpl extends BaseServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveCategory(CategoryParam categoryParam) throws Exception {
        // 校验是否存在同名类别
        Category category = categoryMapper.selectOne(Wrappers.lambdaQuery(Category.class)
                .eq(Category::getName, categoryParam.getName())
                .eq(Category::getShopId, categoryParam.getShopId())
                .eq(Category::getParentId, categoryParam.getParentId()));
        if (category != null) {
            throw new BusinessException("当前分类已存在，请勿重复添加");
        }

        // 保存分类信息
        category = new Category();
        BeanUtils.copyProperties(categoryParam, category);
        return super.save(category);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateCategory(CategoryParam categoryParam) throws Exception {
        // 获取当前分类信息
        Category category = categoryMapper.selectById(categoryParam.getId());
        if (category == null) {
            throw new BusinessException("分类信息异常");
        }

        // 校验修改的名称是否和已有的重复
        Category check = categoryMapper.selectOne(Wrappers.lambdaQuery(Category.class)
                .eq(Category::getName, categoryParam.getName())
                .eq(Category::getShopId, categoryParam.getShopId())
                .eq(Category::getParentId, categoryParam.getParentId())
                .ne(Category::getId, categoryParam.getId()));
        if (check != null) {
            throw new BusinessException("当前分类名称已存在");
        }

        // 更新分类信息
        BeanUtils.copyProperties(categoryParam, category);
        return super.updateById(category);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteCategory(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public Paging<CategoryVo> getCategoryPageList(CategoryPageParam categoryPageParam) throws Exception {
        Page<Category> page = new PageInfo<>(categoryPageParam);
//        if (categoryPageParam.getShopId() == null) {
//            categoryPageParam.setShopId(LoginUtil.getShopId());
//        }
        IPage<CategoryVo> iPage = categoryMapper.getCategoryList(page, categoryPageParam);
        return new Paging<CategoryVo>(iPage);
    }

}
